<?PHP
global $op_modules;
// Define default modules and values
$op_modules = array(
	'phpBB'=>0
);

global $module_help; // Help text.  Needs to be global so we can access from within admin settings callback function.
if(isset($_POST['action']) && $_POST['action'] == 'update_op_admin_settings'){
	if(check_admin_referer('op_admin_update_settings') && current_user_can('manage_options')){  // Security checks
			// Update Modules
			$updatemodules = array();
			if($_POST['modules'] != NULL){
				$postmodules = attribute_escape($_POST['modules']);
				
				$current_modules = get_option('op_modules');
				$current_modules = $current_modules;

				foreach($postmodules as $module => $v){
					//if($current_modules[$module] == 1 && $v == 'on') continue;
					// TODO -- need to cleanup active widgets here
					$load_module = strtolower($module);
					// Load module files
					if(file_exists(TEMPLATEPATH.'/op/modules/module.'.$load_module.'.php')){
						require_once(TEMPLATEPATH.'/op/modules/module.'.$load_module.'.php');
					}
					
					// If module class has an install method, call it
					if(is_callable(array('module_'.$load_module,'install'))){
						$response = call_user_func(array('module_'.$load_module,'install'));
						if($response['error'] == 0){
							$updatemodules[$module] = 1; // module can be activated
						}
						$module_help .= '<div class="module">'.$response['help'].'</div>';
					}
					
				}
			}
			
			if(!is_array($current_modules)) $current_modules = array();
			$updatemodules = array_merge($op_modules,$current_modules,$updatemodules);
			update_option('op_modules',$updatemodules);
			
			// Modules update nav:
			ob_start();  // Use output buffer to suppress errors?
			OP::cache_settings(); // Need to recreate navigation
			ob_end_clean();
			
			// Update header image
			$op_header_img = strlen($_POST['op_header_img']) > 0 && is_numeric($_POST['op_header_img']) ? attribute_escape($_POST['op_header_img']) : 0;
			update_option('op_theme_header_img',$op_header_img);
	}
}
class OP_Admin_Settings{
	function construct(){
		echo '<form method="post" action="">',
			'<h2>1. Modules</h2>',
			'<hr/>',
			
	 	OP_Admin_Settings::modules();
	 	OP_Admin_Settings::theme_header();
	 	
		echo'<p class="submit"><input type="submit" name="Submit" value="';
		_e('Save Changes');
		echo '" /></p>';
		wp_nonce_field('op_admin_update_settings');
		echo '<input type="hidden" name="action" value="update_op_admin_settings" />';
		
		echo '</div>',
			'</form>';
	 	
	}
	
	function modules(){
		// 1. Handle Modules
		global $op_modules;
		
		// Validate modules
		$available_modules = array();
		foreach($op_modules as $module => $v){
			switch($module){
				case 'phpBB':
					//$remove = !file_exists(ABSPATH.'forum/config.php') ? TRUE : FALSE;
					$remove = FALSE;
					break;
			}
			
			if(!$remove)
				$available_modules[$module] = 0;
		}
	
		if (!get_option('op_modules')){
		  $default = array();
		  add_option('op_modules', $default);
		}
		
		$current_modules = get_option('op_modules');
		// Get cross section of modules available and modules with user stored values
		$modules = array_merge($available_modules,$current_modules);
		
		$blog_url = get_bloginfo('wpurl');
	
		echo '<div class="setting">';
		
		if(count($modules) > 0){
			global $module_help;
			echo <<<EOS
			<div class="help">$module_help</div>
			<p>Check modules to activate.</p>
EOS;
			foreach($modules as $k => $v){
				$checked = $v ? ' CHECKED' : '';
				echo '<input type="checkbox" name="modules['.$k.']" '.$checked.'/> '.$k.'<br/>';
				
				if($k == 'phpBB'){
					if(!$path = get_option('op_module_phpbb'))
						$path = 'forum/';
					echo 'Choose path for phpBB: <input type="text" value="'.$path.'" name="phpbb_path"/><br/>';
					if(!$tab = get_option('op_module_phpbb_tab'))
						$tab = 'Community';
					echo 'Choose tab text for phpBB: <input type="text" value="'.$tab.'" name="phpbb_tab"/>';
				}
			}
		}
		else{
			echo '<p>There are currently no modules available for activation</p>';
		}
		
		echo '</div>';
		
	}
	
	function theme_header(){
		if (!$current_img = get_option('op_theme_header_img')){
		  $current_img = 0;
		  add_option('op_theme_header_img', $current_img);
		}
		
		// 2. Handle Theme Header
		echo '<h2>2. Theme Header Image</h2>';
		echo '<hr/>',
			'<p><a href="media-new.php">Add New Image</a> (Note: Only images over 800px wide will be available for the theme header image.)</p>',
			'<div class="setting">';
			
		global $wpdb;
		$all_att = $wpdb->get_results("SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'attachment'");
		
		echo '<div id="images_available">';
		
		// Handle default image
		$default_img = get_bloginfo('template_directory').'/op/admin/images/clear_image.png';
		$class = $current_img == 0 ? 'class="selected" ' : '';
		echo '<div class="op_image_selection_img" id="op_img-0"><img src="'.$default_img.'"'.$class.'/></div>';
		
		foreach($all_att as $att){
			$meta = get_post_meta($att->ID,'_wp_attachment_metadata');
			if(!is_array($meta) || $meta[0]['width'] < 800) continue;
			if($files = wp_get_attachment_image_src($att->ID)){
				
				$class = $att->ID == $current_img ? 'class="selected" ' : '';
				echo '<div class="op_image_selection_img" id="op_img-'.$att->ID.'"><img '.$class.'src="'.$files[0].'" /></div>';
			}
		}
		echo '</div>';
		echo '<input type="hidden" name="op_header_img" id="op_header_img">';
		echo '</div>';
		
	}
	 
	function add_styles(){
		$url = get_bloginfo('template_directory').'/op/admin/';
		wp_register_style('opadminsettings',$url.'styles/admin_settings.css');
		wp_enqueue_style('opadminsettings');
		wp_register_script('opadminsettingsjs', $url.'scripts/admin_settings.js',array('jquery','jquery-ui-sortable'));
		wp_enqueue_script('opadminsettingsjs');		
	}
}


?>